package com.serverclient.cglib.beanregister;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import com.serverclient.cglib.annotation.EnableCglibServerClient;
import com.serverclient.cglib.annotation.ServiceBean;
import com.serverclient.cglib.service.ServerCglibService;
import com.serverclient.utility.PackageScanUtility;

public class BeanRegister implements ImportBeanDefinitionRegistrar {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		//获取指定的注解
		Map<String,Object> map = importingClassMetadata.getAnnotationAttributes(EnableCglibServerClient.class.getName());
		//获取注解中的指定项的值
		String[] packages = (String[])map.get("packages");
		Set<Class<?>> set = PackageScanUtility.getClasses(packages,ServiceBean.class);
		for(Class<?> clazz : set) {
			BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
			AbstractBeanDefinition definition = builder.getBeanDefinition();
			definition.setBeanClass(ServerCglibService.class);
			registry.registerBeanDefinition(clazz.getAnnotation(ServiceBean.class).value(), definition);
		}
	}
}
